/*******************************************************************************
 * Copyright (c) 2014, MD PnP Program
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ******************************************************************************/
package org.mdpnp.devices.masimo.radical;

import java.io.IOException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.mdpnp.devices.DeviceClock;
import org.mdpnp.devices.io.ASCIIFieldDelegate;

/**
 * @author Jeff Plourde
 *
 */
public class MasimoRadical7 extends ASCIIFieldDelegate implements DeviceClock {

    private Integer heartRate, spo2, perfusionIndex, spco, spmet, desat, pidelta, pvi;
    private String guid, alarm;
    private Date lastPoint=new Date(0);

    public MasimoRadical7() throws IOException, NoSuchFieldException, SecurityException {
        super(MasimoRadical7.class.getResource("masimo-radical-7.spec"));
    }

    private static final Pattern number = Pattern.compile("^(\\d+)$");

    @SuppressWarnings("unused")
    private static final String filter(String val) {
        if (null == val) {
            return null;
        }
        Matcher m1 = number.matcher(val);
        if (m1.matches()) {
            return m1.group(1);
        } else {
            return null;
        }
    }

    public void firePulseOximeter() {
        // Fired when all fields have been set
    }

    public Integer getHeartRate() {
        return heartRate;
    }

    public Integer getSpO2() {
        return spo2;
    }

    public Integer getDesat() {
        return desat;
    }

    public Integer getPerfusionIndex() {
        return perfusionIndex;
    }

    public Integer getPIDelta() {
        return pidelta;
    }

    public Integer getPlethVariabilityIndex() {
        return pvi;
    }

    public Integer getSpCO() {
        return spco;
    }

    public Integer getSpMet() {
        return spmet;
    }

    @Override
    public Reading instant() {
        return new ReadingImpl(lastPoint.getTime());
    }

    public String getUniqueId() {
        return guid;
    }
    
    public String getAlarm() {
        return alarm;
    }
}
